package com.joinway.mobile.controller;

import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;

import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiQueryParam;
import org.jsondoc.core.annotation.ApiResponseObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.joinway.framework.bean.logging.annotation.InputLog;
import com.joinway.framework.bean.logging.annotation.LogIgnore;
import com.joinway.framework.bean.logging.annotation.OutputLog;
import com.joinway.framework.bean.logging.type.LogFormat;
import com.joinway.framework.bean.view.View;
import com.joinway.framework.core.controller.ExceptionController;
import com.joinway.framework.support.audit.annotation.Audit;
import com.joinway.framework.support.input.annotation.Render;
import com.joinway.framework.support.input.type.Case;
import com.joinway.mobile.bean.view.UserCheckoutView;
import com.joinway.mobile.bean.view.UserView;
import com.joinway.mobile.service.UserService;


@Api(name = "用户接口", description = "手机用户相关接口")
@RestController
@RequestMapping("user")
@Validated
public class UserController extends ExceptionController {

	@Autowired UserService service;
	
	@ApiMethod(description="获得用户信息", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
//	@ApiErrors(apierrors={
//			@ApiError(code=ErrorCodeConstants.INTERNAL_ERROR, description=ErrorCodeConstants.INTERNAL_ERROR_DESC)
//			, @ApiError(code=ErrorCodeConstants.INVALID_INPUT, description=ErrorCodeConstants.INVALID_INPUT_DESC)
//
//		}
//	)
	@RequestMapping(value="/profile.json", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public UserView getProfile(@Min(1) @RequestParam("userId") @Render @ApiQueryParam(name="userId", description="用户id") int userId) throws Exception {
		return service.getProfile(userId);
	}

	@ApiMethod(description="更新用户信息", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
//	@ApiErrors(apierrors={
//			@ApiError(code=ErrorCodeConstants.INTERNAL_ERROR, description=ErrorCodeConstants.INTERNAL_ERROR_DESC)
//			, @ApiError(code=ErrorCodeConstants.INVALID_INPUT, description=ErrorCodeConstants.INVALID_INPUT_DESC)
//
//		}
//	)
	@RequestMapping(value="/profile.json", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public View updateProfile(
			@Min(1) @RequestParam("userId") @Render @ApiQueryParam(name="userId", description="用户id") int userId
			, @RequestParam(value="name",required=false,defaultValue="") @Render @ApiQueryParam(name="name", description="真实姓名") @LogIgnore String name
			, @Pattern(regexp="[FMS]{1}") @RequestParam(value="gender",required=false,defaultValue="M") @Render(format=Case.Upper) @ApiQueryParam(name="gender", description="性别,F-女;M-男;S-保密", allowedvalues={"F","M","S"}) String gender
//			, @RequestParam(value="address",required=false,defaultValue="") @Render @ApiQueryParam(name="address", description="收货地址") String address
//			, @RequestParam(value="postalCode",required=false,defaultValue="") @Render @ApiQueryParam(name="postalCode", description="邮政编码") String postalCode
//			, @RequestParam(value="email",required=false,defaultValue="") @Render @ApiQueryParam(name="email", description="电子邮箱") String email
//			, @RequestParam(value="qq",required=false,defaultValue="") @Render @ApiQueryParam(name="qq", description="qq号码") String qq
	) throws Exception {
		return service.updateProfile(userId, name, gender);
	}
	
	@ApiMethod(description="用户签到", produces=MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value="/checkout.json", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public UserCheckoutView checkout(
			@Min(1) @RequestParam("userId") @Render @ApiQueryParam(name="userId", description="用户id") int userId
			) throws Exception {
		return service.checkout(userId);
	}
	
}
